#include "StdAfx.h"
#include "XMLDoc.h"


CXMLDoc::CXMLDoc(void)
{
	m_doc = new tinyxml2::XMLDocument();
}


CXMLDoc::~CXMLDoc(void)
{
	if(m_doc)
		delete m_doc;
}

bool CXMLDoc::initialize()
{
	return m_doc?true:false;
}

bool CXMLDoc::LoadFile(const char* file)
{
	tinyxml2::XMLError error = m_doc->LoadFile(file);
	if(error != XML_SUCCESS)
		return false;
	return true;
}

bool CXMLDoc::LoadFile( FILE* fp)
{
	tinyxml2::XMLError error = m_doc->LoadFile(fp);
	if(error != XML_SUCCESS)
		return false;
	return true;
}

XMLNode* CXMLDoc::getRoot()
{
	return m_doc;
}

XMLNode* CXMLDoc::insertSibling(XMLNode* node, const char*  content, bool isGenerateName)
{
	XMLNode* pNode = node->Parent();
	if(!pNode) return NULL;
	
	XMLElement* element = NULL;
	if(isGenerateName)
		element = m_doc->NewElement("item");
	else
		element = m_doc->NewElement("number");
	element->SetAttribute( "content", content);

	pNode->InsertAfterChild(node, element);
	return element;
}

XMLNode* CXMLDoc::insertSub(XMLNode* node, const char* content, bool isGenerateName)
{
	XMLElement* element = NULL;
	if(isGenerateName)
		element = m_doc->NewElement("item");
	else
		element = m_doc->NewElement("number");
	element->SetAttribute( "content", content);
	if(!node)
		node = getRoot();
	node->InsertEndChild(element);
	return element;
}

bool CXMLDoc::saveFile(const char* filePath)
{
	m_doc->Print();
	XMLError error = m_doc->SaveFile(filePath);
	return error == XML_SUCCESS?true:false;
}

bool CXMLDoc::saveFile(FILE* fp)
{
	m_doc->Print();
	XMLError error = m_doc->SaveFile(fp);
	return error == XML_SUCCESS?true:false;
}

std::string CXMLDoc::generateListNumberString(XMLNode* node)
{
	return "";
}
